package com.dapeis.service.order;

import com.ag.core.exception.SysTipsException;
import com.ag.core.utils.AgAssert;
import com.ag.utils.linked.LinkedList;
import com.ag.utils.linked.Node;
import com.dapeis.core.utils.StylistConstants;
import com.dapeis.domain.Order;
import com.dapeis.exception.order.IllegalOrderStateException;

/**
 *订单流程 
 * Title:OrderFlow.java OrderFlow
 * Description:
 * @author yuanyong
 * @date 2017年5月11日 上午9:09:23 
 * Email: hzyuanyong@126.com
 *
 */
public abstract class OrderFlow {
    
    protected LinkedList flow = new LinkedList();
	
	public OrderFlow() {
		initFlow();
	}
	public abstract String getOrderType();
	public abstract void initFlow();
	
	public LinkedList get() {
		return this.flow;
	}
	public void check(Order order, int equalSatus) {
		if(StylistConstants.Order.Status.COLSED == order.getState()
				|| StylistConstants.Order.Status.CANEL == order.getState()
				|| StylistConstants.Order.Status.FINISHED == order.getState()) {
			throw new IllegalOrderStateException("订单已经不是处理中的订单.", order.getState(), equalSatus);
		}
		if(order.getState() != equalSatus) {
			throw new IllegalOrderStateException(order.getState(), equalSatus);
		}
	}
	
	/**
	 * 验证订单的下一步骤的有效性
	 * @param currStatus
	 * @param status
	 */
	public void checkOrderForwardFlowValidity(int currStatus, int status) {
		Node node = flow.getNodeByValue(currStatus);
		AgAssert.notNull(node, SysTipsException.class, "找不到订单流程:" + status);
		
		boolean equals = node.getNext().getValue().equals(status);
		if(!equals) {
			throw new IllegalOrderStateException(currStatus, status);
		}
	}
	
	public int next(Object v) {
		Node node = flow.getNodeByValue(v);
		if(node == null) {
			throw new RuntimeException("next.cur is nil.");
		}
		
		Node next = node.getNext();
		if(next == null) {
			throw new RuntimeException("next is nil.");
		}
		return (int) next.getValue();
	}

}

